package com.vrv.app.account;

import java.io.File;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.app.R;
import com.vrv.common.Constants;
import com.vrv.common.utils.FileUtils;
import com.vrv.common.utils.StringUtils;

public class MyAccountActivity extends Activity {
    /**
     * 账号
     */
    private TextView loginIdTxt;
    /**
     * 性别
     */
    private TextView genderTxt;
    /**
     * 邮箱
     */
    private TextView emailTxt;
    /**
     *
     */
    private ImageView faceImage = null;
    /**
     * 头像文件名称(UUID)
     */
    private String headImageUUIDName = null;

    private static final int IMAGE_REQUEST_CODE = 0;// 请求码
    private static final int CAMERA_REQUEST_CODE = 1;// 请求码
    private static final int RESULT_REQUEST_CODE = 2;// 请求码

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.my_account);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.account_titlebar);

        // 如果对头像操作则使用此名称
        headImageUUIDName = UUID.randomUUID().toString().replace("-", "") + ".png";
        findViews();
    }

    private void findViews() {
        TextView title = (TextView) findViewById(R.id.title_text);
        title.setText("我的账号");
        title.getPaint().setFakeBoldText(true);

        /**
         * 返回按钮
         */
        ImageButton gobackBtn = (ImageButton) findViewById(R.id.gobackBtn);
        gobackBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        /**
         * 更多按钮
         */
        ImageButton modifyBtn = (ImageButton) findViewById(R.id.moreBtn);
        modifyBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoAccountInfoModify();
            }
        });

        // 昵称
        TextView nameTxt = (TextView) findViewById(R.id.nameTxt);
        nameTxt.setText("昵称");
        nameTxt.getPaint().setFakeBoldText(true);
        // 登录账号
        loginIdTxt = (TextView) findViewById(R.id.loginIdTxt);
        loginIdTxt.setText(Constants.user.getLoginId());
        loginIdTxt.getPaint().setFakeBoldText(true);
        // 性别
        genderTxt = (TextView) findViewById(R.id.genderTxt);
        genderTxt.setText("男");
        genderTxt.getPaint().setFakeBoldText(true);
        // 邮箱
        emailTxt = (TextView) findViewById(R.id.emailTxt);
        emailTxt.setText("abc@123.com");
        emailTxt.getPaint().setFakeBoldText(true);
        // 头像
        faceImage = (ImageView) findViewById(R.id.faceImage);
        faceImage.setImageResource(R.drawable.default_comment_user_man_icon);
    }

    /**
     * 菜单点击事件
     */
    OnClickListener menuOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.menu_item2Layout:
                gotoAccountInfoModify();
                break;
            }
        }
    };

    private boolean hasErrors = Boolean.FALSE;

    private EditText nameEditTxt;
    private EditText emailEditTxt;

    @SuppressWarnings("unused")
    private String gender = null;
    private RadioButton maleRadio = null;
    private RadioButton femaleRadio = null;

    private ImageView faceImageModified = null;

    /**
     * 切换到“编辑模式”修改账户信息
     */
    private void gotoAccountInfoModify() {

        setContentView(R.layout.my_account_edit);

        nameEditTxt = (EditText) findViewById(R.id.nameEditTxt);
        nameEditTxt.setText("昵称");
        nameEditTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if (nameEditTxt.getText().toString().length() > 24 || nameEditTxt.getText().toString().length() < 2) {
                    hasErrors = Boolean.TRUE;
                    nameEditTxt.setError("账户名称长度在2-24之间！");
                } else {
                    hasErrors = Boolean.FALSE;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });

        TextView loginIdTxt = (TextView) findViewById(R.id.loginIdTxt);
        loginIdTxt.setText(Constants.user.getLoginId());

        emailEditTxt = (EditText) findViewById(R.id.emailEditTxt);
        emailEditTxt.setText("abc@123.com");
        emailEditTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
                Matcher matcher = pattern.matcher(emailEditTxt.getText().toString());
                if (!matcher.matches()) {
                    hasErrors = Boolean.TRUE;
                    emailEditTxt.setError("请正确输入电子邮件的值！");
                } else {
                    hasErrors = Boolean.FALSE;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });

        // 性别回显
        maleRadio = (RadioButton) findViewById(R.id.radio0);
        femaleRadio = (RadioButton) findViewById(R.id.radio1);
        final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.genderRadioGroup);
        if ("F".equals("M")) {
            femaleRadio.setChecked(Boolean.TRUE);
        } else {
            maleRadio.setChecked(Boolean.TRUE);
        }

        // 性别的监听器
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) radioGroup.findViewById(checkedId);
                gender = radioButton.getText().toString().equals("男") ? "M" : "F";
            }
        });

        // 回显头像
        faceImageModified = (ImageView) findViewById(R.id.faceImage);
        faceImageModified.setImageResource(R.drawable.default_comment_user_man_icon);

        faceImageModified.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // gotoAccountImageModify();
            }
        });

        Button button = (Button) findViewById(R.id.btn_submit_account_info);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isNullOrEmpty(nameEditTxt.getText().toString()) || StringUtils.isNullOrEmpty(emailEditTxt.getText().toString()) || hasErrors) {
                    Toast.makeText(MyAccountActivity.this, "请正确填写各个输入项的值！", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 结果码不等于取消时候
        if (resultCode != RESULT_CANCELED) {
            switch (requestCode) {
            case IMAGE_REQUEST_CODE:
                cropHeadImage(data.getData());
                break;
            case CAMERA_REQUEST_CODE:
                if (FileUtils.hasSDcard()) {
                    File file = new File(FileUtils.getProjectPath() + "account/" + "", headImageUUIDName);
                    cropHeadImage(Uri.fromFile(file));
                } else {
                    Toast.makeText(MyAccountActivity.this, "未找到存储卡,无法存储照片！", Toast.LENGTH_LONG).show();
                }
                break;
            case RESULT_REQUEST_CODE:
                if (data != null) {
                    // saveCropHeadImage(data);
                }
                break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 裁剪头像
     *
     * @param uri
     */
    public void cropHeadImage(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 320);
        intent.putExtra("outputY", 320);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, RESULT_REQUEST_CODE);
        overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
        super.onBackPressed();
    }
}
